<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: membx.php Sat Jul 18 13:13:00 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") ) exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsMemb extends XoopsObject 
{
	public function __construct() {
		$this->initVar("member_id", XOBJ_DTYPE_INT, null, false); // ID
		$this->initVar("uid", XOBJ_DTYPE_INT,0,true); // 用户ID
		$this->initVar("group_id", XOBJ_DTYPE_INT,0,true); // 所属群组ID
		$this->initVar("member_isadmin", XOBJ_DTYPE_INT,0,false); // 0 不是 1 是
		$this->initVar("member_create_time", XOBJ_DTYPE_INT,0,false); // 加入时间
		$this->initVar("member_status", XOBJ_DTYPE_INT,0,false); // 加入状态 1 通过 0 待审核
	}
}

class groupsMembHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_member","groupsMemb","member_id");
    }
    
    /**
     * create a group member
     *
     * @param array $params
     * @return int $member_id
     */
    public function setMember( array $params ) {
        extract($params);
        if ( empty($uid) || empty($group_id) ) return false;
        if ( !empty($member_id) ) {
            $obj = $this->get($member_id);
        } else {
            $obj = $this->get();
            $obj->setVar("member_create_time",time());
        }
    	$obj->setVar("uid",(int)$uid);
    	$obj->setVar("group_id",(int)$group_id);
    	if ( $id = $this->insert($obj) ) {
    	    return $id;
    	}
    	return false;
    }
    
    /**
     * remove user for this group
     *
     * @param int $member_id
     * @return bool $ret
     */
    public function removeMember( array $params ) {
        extract($params);
        if ( empty($uid) || empty($group_id) ) return false;
        $critera = new CriteriaCompo();
        $critera->add(new Criteria("uid",(int)$uid));
        $critera->add(new Criteria("group_id",(int)$group_id));
        $this->deleteAll($critera);
    }
    
    /**
     * update a group admin
     *
     * @param int $member_id
     * @param bool $asAdmin
     * @return int $id
     */
    public function updateToAdmin( $member_id, $asAdmin=false ) {
        $obj = $this->get(intval($member_id));
        if ( !is_object($obj) && $obj->isNew() )  return false;
        $admin_val = ( $asAdmin === true ) ? 1 : 0;
        $obj->setVar("member_isadmin",$admin_val);
        if ( $id = $this->insert($obj) ) {
            return $id;
        }
        return false;
    }
    
    /**
     * update user status for this group 
     *
     * @param int $member_id
     * @param int $val
     * @return int $uid
     */
    public function updateMemberStatus( $member_id, $val=0 ) {
        
    }
        
    /**
     * select user group ids
     *
     * @param int $uid
     * @return array $ids
     */
    public function getUserGroupIds( $uid ) {
        $params = array(
            "criteria" => array(
                "uid"=>$uid,
                "member_status"=> 1
            ),
            "fields" => array("group_id"),
            "order"=>"ASC",
            "sort"=>"member_create_time"
        );
        $criteria = $this->setCriteria($params);
        $objs = $this->getAll($criteria);
        $ret = array();
        if ( empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $ret[] = $obj->getVar("group_id");
            }
        }
        array_unique($ret);
        return $ret;        
    }
    
    /**
     * select member list
     *
     * @param array $params
     * @param bool $asObj
     * @return array $ret array/object array
     */
    public function getMembers( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
    /**
     * for member page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $start, "start",$extra);
        	$pagenav = $nav->renderNav(4);
        }
        return $pagenav;
    }    
}
    